package com.linkai.onlinetool.controller;

import com.alibaba.fastjson.JSON;
import com.linkai.onlinetool.entity.Rgb;
import org.springframework.web.bind.annotation.*;

/*  直接返回字符串     */
@RestController
public class RGBColorController {


    /**
     *      用 @CrossOrigin 注解处理跨域问题
     *      参考：https://jingyan.baidu.com/article/c14654136fd0f24afdfc4c04.html
     * */

    /**
    * @Description: 将RGB颜色转换为16进制
    * @Param: [r, g, b]
    * @return: java.lang.String
    * @Author: 林凯
    * @Date: 2020/10/9
    */
    @GetMapping("/rgbToHexadecimal")
//    @CrossOrigin(value = "http://101.132.147.111:8080", maxAge = 1800, allowedHeaders = "*")
    @CrossOrigin(allowCredentials="true",maxAge = 3600)
    public String rgbColor(@RequestParam("r") int r, @RequestParam("g") int g, @RequestParam("b") int b) {
        // r,g,b 的取值范围为 0~255 ，需要在前端就确定范围
        System.out.println("r = " + r + "  " + "g ==" + g + "  " + "b ==" + b);
        String rFString, rSString, gFString, gSString,
                bFString, bSString, result;

        // 转换方法 2
//        String hex = String.format("#%02X%02X%02X", Integer.parseInt(r),
//                Integer.parseInt(g), Integer.parseInt(b));

        int red, green, blue;
        int rred, rgreen, rblue;
        /*  简单的数学计算，拿 17 举例*/
        red = r / 16;
        rred = r % 16;
        if (red == 10) {
            rFString = "A";
        } else if (red == 11) {
            rFString = "B";
        } else if (red == 12) {
            rFString = "C";
        } else if (red == 13) {
            rFString = "D";
        } else if (red == 14) {
            rFString = "E";
        } else if (red == 15) {
            rFString = "F";
        } else {
            rFString = String.valueOf(red);
        }

        if (rred == 10) {
            rSString = "A";
        } else if (rred == 11) {
            rSString = "B";
        } else if (rred == 12) {
            rSString = "C";
        } else if (rred == 13) {
            rSString = "D";
        } else if (rred == 14) {
            rSString = "E";
        } else if (rred == 15) {
            rSString = "F";
        } else {
            rSString = String.valueOf(rred);
        }

        rFString = rFString + rSString;

        green = g / 16;
        rgreen = g % 16;

        if (green == 10) {
            gFString = "A";
        } else if (green == 11) {
            gFString = "B";
        } else if (green == 12) {
            gFString = "C";
        } else if (green == 13) {
            gFString = "D";
        } else if (green == 14) {
            gFString = "E";
        } else if (green == 15) {
            gFString = "F";
        } else {
            gFString = String.valueOf(green);
        }

        if (rgreen == 10) {
            gSString = "A";
        } else if (rgreen == 11) {
            gSString = "B";
        } else if (rgreen == 12) {
            gSString = "C";
        } else if (rgreen == 13) {
            gSString = "D";
        } else if (rgreen == 14) {
            gSString = "E";
        } else if (rgreen == 15) {
            gSString = "F";
        } else {
            gSString = String.valueOf(rgreen);
        }

        gFString = gFString + gSString;

        blue = b / 16;
        rblue = b % 16;

        if (blue == 10) {
            bFString = "A";
        } else if (blue == 11) {
            bFString = "B";
        } else if (blue == 12) {
            bFString = "C";
        } else if (blue == 13) {
            bFString = "D";
        } else if (blue == 14) {
            bFString = "E";
        } else if (blue == 15) {
            bFString = "F";
        } else {
            bFString = String.valueOf(blue);
        }

        if (rblue == 10) {
            bSString = "A";
        } else if (rblue == 11) {
            bSString = "B";
        } else if (rblue == 12) {
            bSString = "C";
        } else if (rblue == 13) {
            bSString = "D";
        } else if (rblue == 14) {
            bSString = "E";
        } else if (rblue == 15) {
            bSString = "F";
        } else {
            bSString = String.valueOf(rblue);
        }
        bFString = bFString + bSString;
        result = "#" + rFString + gFString + bFString;
        return result;
    }


    /**
    * @Description: 将16进制数转换为RGB
    * @Param: []
    * @return: java.lang.String
    * @Author: 林凯
    * @Date: 2020/10/9
    */
    @GetMapping("/hexadecimalToRgb")
//    @CrossOrigin(value = "http://101.132.147.111:8080", maxAge = 1800, allowedHeaders = "*")
    @CrossOrigin(allowCredentials="true",maxAge = 3600)
    public String numberToRGB(@RequestParam("hex") String hex) {
        // hex 的值举例： #fFff0A    大小写都可以
        System.out.println("hex = " + hex);
        String str2 = hex.substring(1,3);
        String str3 = hex.substring(3,5);
        String str4 = hex.substring(5,7);
        int red = Integer.parseInt(str2,16);
        int green = Integer.parseInt(str3,16);
        int blue = Integer.parseInt(str4,16);
        System.out.println("red =" + red + " green=" + green + "  blue=" + blue);

        Rgb rgb = new Rgb(red, green, blue);
        System.out.println("rgb = " + rgb);


        String result = JSON.toJSONString(rgb);
        System.out.println("result = " + result);

        return result;
    }

}
